import 'package:online_bookstore/models/book.dart';
import 'package:online_bookstore/models/user.dart';
import 'package:online_bookstore/models/order.dart';
import 'package:online_bookstore/models/cart_item.dart';

class MockDataService {
  static final MockDataService _instance = MockDataService._internal();
  factory MockDataService() => _instance;
  MockDataService._internal();

  // Mock Books Data
  static final List<Book> mockBooks = [
    Book(
      id: '1',
      title: 'The Great Gatsby',
      author: 'F. Scott Fitzgerald',
      description: 'A classic American novel set in the Jazz Age, following the mysterious Jay Gatsby and his obsession with the beautiful Daisy Buchanan.',
      isbn: '9780743273565',
      price: 12.99,
      coverImageUrl: 'https://covers.openlibrary.org/b/id/8221461-L.jpg',
      category: 'Fiction',
      rating: 4.2,
      reviewCount: 1250,
      stock: 15,
      publishedDate: DateTime(1925, 4, 10),
      tags: ['Classic', 'American Literature', 'Jazz Age'],
      publisher: 'Scribner',
      pageCount: 180,
      language: 'English',
    ),
    Book(
      id: '2',
      title: 'To Kill a Mockingbird',
      author: 'Harper Lee',
      description: 'A gripping tale of racial injustice and childhood innocence in the American South during the 1930s.',
      isbn: '9780061120084',
      price: 14.99,
      coverImageUrl: 'https://covers.openlibrary.org/b/id/8221462-L.jpg',
      category: 'Fiction',
      rating: 4.5,
      reviewCount: 2100,
      stock: 8,
      publishedDate: DateTime(1960, 7, 11),
      tags: ['Classic', 'American Literature', 'Social Justice'],
      publisher: 'J.B. Lippincott & Co.',
      pageCount: 281,
      language: 'English',
    ),
    Book(
      id: '3',
      title: '1984',
      author: 'George Orwell',
      description: 'A dystopian social science fiction novel about totalitarian control and surveillance.',
      isbn: '9780451524935',
      price: 13.99,
      coverImageUrl: 'https://covers.openlibrary.org/b/id/8221463-L.jpg',
      category: 'Fiction',
      rating: 4.3,
      reviewCount: 1800,
      stock: 12,
      publishedDate: DateTime(1949, 6, 8),
      tags: ['Dystopian', 'Science Fiction', 'Political'],
      publisher: 'Secker & Warburg',
      pageCount: 328,
      language: 'English',
    ),
    Book(
      id: '4',
      title: 'Pride and Prejudice',
      author: 'Jane Austen',
      description: 'A romantic novel of manners that critiques the British landed gentry of the early 19th century.',
      isbn: '9780141439518',
      price: 11.99,
      coverImageUrl: 'https://covers.openlibrary.org/b/id/8221464-L.jpg',
      category: 'Romance',
      rating: 4.4,
      reviewCount: 1650,
      stock: 20,
      publishedDate: DateTime(1813, 1, 28),
      tags: ['Romance', 'Classic', 'British Literature'],
      publisher: 'T. Egerton, Whitehall',
      pageCount: 432,
      language: 'English',
    ),
    Book(
      id: '5',
      title: 'The Catcher in the Rye',
      author: 'J.D. Salinger',
      description: 'A coming-of-age story about teenage rebellion and alienation in 1950s America.',
      isbn: '9780316769174',
      price: 13.49,
      coverImageUrl: 'https://covers.openlibrary.org/b/id/8221465-L.jpg',
      category: 'Fiction',
      rating: 3.8,
      reviewCount: 950,
      stock: 6,
      publishedDate: DateTime(1951, 7, 16),
      tags: ['Coming of Age', 'American Literature', 'Teenage'],
      publisher: 'Little, Brown and Company',
      pageCount: 277,
      language: 'English',
    ),
    Book(
      id: '6',
      title: 'The Lord of the Rings',
      author: 'J.R.R. Tolkien',
      description: 'An epic high-fantasy novel about the quest to destroy the One Ring and defeat the Dark Lord Sauron.',
      isbn: '9780544003415',
      price: 19.99,
      coverImageUrl: 'https://covers.openlibrary.org/b/id/8221466-L.jpg',
      category: 'Fantasy',
      rating: 4.7,
      reviewCount: 3200,
      stock: 25,
      publishedDate: DateTime(1954, 7, 29),
      tags: ['Fantasy', 'Adventure', 'Epic'],
      publisher: 'Allen & Unwin',
      pageCount: 1216,
      language: 'English',
    ),
    Book(
      id: '7',
      title: 'Harry Potter and the Philosopher\'s Stone',
      author: 'J.K. Rowling',
      description: 'The first book in the Harry Potter series, following a young wizard\'s journey at Hogwarts School.',
      isbn: '9780747532699',
      price: 16.99,
      coverImageUrl: 'https://covers.openlibrary.org/b/id/8221467-L.jpg',
      category: 'Fantasy',
      rating: 4.6,
      reviewCount: 4500,
      stock: 30,
      publishedDate: DateTime(1997, 6, 26),
      tags: ['Fantasy', 'Magic', 'Young Adult'],
      publisher: 'Bloomsbury',
      pageCount: 223,
      language: 'English',
    ),
    Book(
      id: '8',
      title: 'The Hobbit',
      author: 'J.R.R. Tolkien',
      description: 'A fantasy novel about a hobbit\'s unexpected journey to help dwarves reclaim their mountain home.',
      isbn: '9780547928227',
      price: 15.99,
      coverImageUrl: 'https://covers.openlibrary.org/b/id/8221468-L.jpg',
      category: 'Fantasy',
      rating: 4.5,
      reviewCount: 2800,
      stock: 18,
      publishedDate: DateTime(1937, 9, 21),
      tags: ['Fantasy', 'Adventure', 'Children\'s Literature'],
      publisher: 'Allen & Unwin',
      pageCount: 310,
      language: 'English',
    ),
    Book(
      id: '9',
      title: 'The Chronicles of Narnia',
      author: 'C.S. Lewis',
      description: 'A series of fantasy novels about children who discover the magical world of Narnia.',
      isbn: '9780064471190',
      price: 17.99,
      coverImageUrl: 'https://covers.openlibrary.org/b/id/8221469-L.jpg',
      category: 'Fantasy',
      rating: 4.3,
      reviewCount: 2100,
      stock: 14,
      publishedDate: DateTime(1950, 10, 16),
      tags: ['Fantasy', 'Children\'s Literature', 'Adventure'],
      publisher: 'Geoffrey Bles',
      pageCount: 767,
      language: 'English',
    ),
    Book(
      id: '10',
      title: 'The Alchemist',
      author: 'Paulo Coelho',
      description: 'A philosophical novel about a young Andalusian shepherd\'s journey to find his personal legend.',
      isbn: '9780061122415',
      price: 14.49,
      coverImageUrl: 'https://covers.openlibrary.org/b/id/8221470-L.jpg',
      category: 'Philosophy',
      rating: 4.1,
      reviewCount: 1200,
      stock: 22,
      publishedDate: DateTime(1988, 1, 1),
      tags: ['Philosophy', 'Self-Help', 'Adventure'],
      publisher: 'HarperCollins',
      pageCount: 163,
      language: 'English',
    ),
  ];

  // Mock Categories
  static final List<String> mockCategories = [
    'Fiction',
    'Romance',
    'Fantasy',
    'Mystery',
    'Science Fiction',
    'Biography',
    'History',
    'Philosophy',
    'Self-Help',
    'Business',
    'Technology',
    'Art',
    'Poetry',
    'Drama',
    'Children\'s Books',
  ];

  // Mock User
  static final User mockUser = User(
    id: 'user_1',
    email: 'john.doe@example.com',
    name: 'John Doe',
    profileImageUrl: 'https://via.placeholder.com/150',
    phoneNumber: '+1-555-0123',
    defaultAddress: const Address(
      id: 'addr_1',
      street: '123 Main Street',
      city: 'New York',
      state: 'NY',
      zipCode: '10001',
      country: 'United States',
      isDefault: true,
    ),
    favoriteCategories: ['Fiction', 'Fantasy', 'Mystery'],
    createdAt: DateTime(2023, 1, 15),
    lastLoginAt: DateTime.now(),
    isEmailVerified: true,
  );

  // Mock Orders
  static final List<Order> mockOrders = [
    Order(
      id: 'order_1',
      userId: 'user_1',
      items: [
        CartItem(
          id: 'cart_1',
          book: mockBooks[0],
          quantity: 1,
          addedAt: DateTime.now().subtract(const Duration(days: 5)),
        ),
        CartItem(
          id: 'cart_2',
          book: mockBooks[1],
          quantity: 2,
          addedAt: DateTime.now().subtract(const Duration(days: 5)),
        ),
      ],
      subtotal: 42.97,
      tax: 3.44,
      shipping: 5.99,
      total: 52.40,
      status: OrderStatus.delivered,
      shippingAddress: mockUser.defaultAddress!,
      createdAt: DateTime.now().subtract(const Duration(days: 5)),
      updatedAt: DateTime.now().subtract(const Duration(days: 1)),
    ),
  ];

  // Helper methods
  static List<Book> getBooksByCategory(String category) {
    if (category == 'All') return mockBooks;
    return mockBooks.where((book) => book.category == category).toList();
  }

  static List<Book> searchBooks(String query) {
    if (query.isEmpty) return mockBooks;
    final lowercaseQuery = query.toLowerCase();
    return mockBooks.where((book) {
      return book.title.toLowerCase().contains(lowercaseQuery) ||
          book.author.toLowerCase().contains(lowercaseQuery) ||
          book.tags.any((tag) => tag.toLowerCase().contains(lowercaseQuery)) ||
          book.category.toLowerCase().contains(lowercaseQuery);
    }).toList();
  }

  static List<Book> getFeaturedBooks() {
    return mockBooks.where((book) => book.rating >= 4.5).take(6).toList();
  }

  static Book? getBookById(String id) {
    try {
      return mockBooks.firstWhere((book) => book.id == id);
    } catch (e) {
      return null;
    }
  }
}
